package com.tianyang.yukuaifu.common.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.tianyang.yukuaifu.R;


/**
 * Description : 通用对话框
 */
public class CommonDialogView extends Dialog {

    // 编辑标题
    private TextView title, content;

    private Context mContext;

    // 确定
    public TextView confirm;

    // 取消
    public TextView cancel;

    private EditText input;

    public DialogViews_ask mAction_ask;

    public interface DialogViews_ask {
        void doOk();

        void doCancle();

        class DialogViews_askImpl implements DialogViews_ask{

            @Override
            public void doOk() {

            }

            public void doOk(String text) {

            }

            @Override
            public void doCancle() {

            }
        }
    }

    /**
     * 实例化
     */
    private void initView() {
        // 编辑标题
        title = (TextView)findViewById(R.id.tv_dialog_title);
        content = (TextView)findViewById(R.id.tv_dialog_content);
        confirm = (TextView)findViewById(R.id.tv_dialog_confirm);
        cancel = (TextView)findViewById(R.id.tv_dialog_cancle);
        input = (EditText)findViewById(R.id.et_dialog_input);
    }

    /**
     * 不带标题
     *
     * @param context
     * @param showtwo
     */
    public CommonDialogView(Context context, boolean showtwo, String message,
                            DialogViews_ask listener) {
        super(context, R.style.dialog_normal);
        createDialog(context,false,showtwo,null,message,false,null,listener);
    }

    /**
     * 带标题
     *
     * @param context
     * @param showtwo false 一个按钮 true 两个按钮
     */
    public CommonDialogView(Context context, boolean showtwo, String name, String message,
                            DialogViews_ask listener) {
        super(context, R.style.dialog_normal);
        createDialog(context,false,showtwo,name,message,false,null,listener);
    }

    /**
     * 带标题,带输入框
     *
     * @param context
     * @param showtwo false 一个按钮 true 两个按钮
     * @param hintContent 输入框提示文字
     * @param passwordInput 是否为密码输入框
     */
    public CommonDialogView(Context context, boolean passwordInput, boolean showtwo, String name, String message,
                            boolean isShowEt, String hintContent, DialogViews_ask listener) {
        super(context, R.style.dialog_normal);
        createDialog(context,passwordInput,showtwo,name,message,isShowEt,hintContent,listener);
    }

    /**
     * 创建具体dialog实例
     * @param context 上下文
     * @param passwordInput 输入框文字
     * @param showtwo 是不是两个按钮，如果不是隐藏取消按钮
     * @param name 标题
     * @param message 内容
     * @param isShowEt 是否显示输入文本框
     * @param hintContent 文本框默认提示
     * @param listener 回调监听
     */
    private void createDialog(Context context, boolean passwordInput, boolean showtwo, String name, String message,
                              boolean isShowEt, String hintContent, DialogViews_ask listener){
        this.mContext = context;
        setContentView(R.layout.common_dialog);
        this.mAction_ask = listener;
        initView();
        if(name!=null) {
            title.setVisibility(View.VISIBLE);
            title.setText(name);
        }
        content.setText(message);
        if (!showtwo) {
            confirm.setText(R.string.known);
            cancel.setVisibility(View.GONE);
        }
        if (isShowEt) {
            input.setVisibility(View.VISIBLE);
            input.setHint(hintContent);
            input.setInputType(passwordInput?
                    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD :
                    InputType.TYPE_CLASS_TEXT);
        }
        // 数据填充
        setDataAndListener();
    }

    /**
     * 数据填充和添加监听
     */
    private void setDataAndListener() {
        // 确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
                if (mAction_ask != null) {
                    if (mAction_ask instanceof DialogViews_ask.DialogViews_askImpl){
                        ((DialogViews_ask.DialogViews_askImpl) mAction_ask).doOk(getEditText());
                    }else{
                        mAction_ask.doOk();
                    }
                }
            }
        });
        // 取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
                if (mAction_ask != null) {
                    mAction_ask.doCancle();
                }
            }
        });
    }

    /**
     * 关闭dialog
     */
    public void close() {
        if (!((Activity)mContext).isFinishing()) {
            ((Activity)mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (isShowing()) {
                        dismiss();
                    }
                }
            });
        }
    }

    public String getEditText() {
        return input.getText().toString().trim();
    }
}
